//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VanillaStorageOption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/BermudanExercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/OneAssetOption.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CVanillaStorageOption::CVanillaStorageOption (Cephei::QL::IBermudanExercise^ ex, Double capacity, Double load, Double changeRate, Cephei::QL::IPricingEngine^ QL_Pricer) : COneAssetOption(CVanillaStorageOption::typeid)
{
    CBermudanExercise^ _Cex;
    try
    {
#ifdef HANDLE
        _phVanillaStorageOption = NULL;
#endif
        _Cex = safe_cast<CBermudanExercise^> (ex);
        _Cex->Lock();
        boost::shared_ptr<QuantLib::BermudanExercise>& _ex = static_cast<boost::shared_ptr<QuantLib::BermudanExercise>&> (_Cex->GetShared ()); 
        QuantLib::Real _capacity = (QuantLib::Real)ValueHelper::Convert (capacity); //d
        QuantLib::Real _load = (QuantLib::Real)ValueHelper::Convert (load); //d
        QuantLib::Real _changeRate = (QuantLib::Real)ValueHelper::Convert (changeRate); //d
        _ppVanillaStorageOption = new boost::shared_ptr<QuantLib::VanillaStorageOption> (new QuantLib::VanillaStorageOption ( _ex,  _capacity,  _load,  _changeRate ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppVanillaStorageOption)->setPricingEngine (_QL_Pricer);
        SetOneAssetOption (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaStorageOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cex != nullptr) _Cex->Unlock();
    }
}
Cephei::QL::Instruments::CVanillaStorageOption::CVanillaStorageOption (boost::shared_ptr<QuantLib::VanillaStorageOption>& childNative, Object^ owner) : COneAssetOption(CVanillaStorageOption::typeid)
{
#ifdef HANDLE
	_phVanillaStorageOption = NULL;
#endif
	_ppVanillaStorageOption = &childNative;
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaStorageOption));
}
Cephei::QL::Instruments::CVanillaStorageOption::CVanillaStorageOption (QuantLib::VanillaStorageOption& childNative, Object^ owner) : COneAssetOption(CVanillaStorageOption::typeid)
{
#ifdef HANDLE
	_phVanillaStorageOption = NULL;
#endif
	_ppVanillaStorageOption = new boost::shared_ptr<QuantLib::VanillaStorageOption> (&childNative);
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaStorageOption));
    _VanillaStorageOptionOwner = owner;
    _OneAssetOptionOwner = owner;
}

Cephei::QL::Instruments::CVanillaStorageOption::CVanillaStorageOption (CVanillaStorageOption^ copy) : COneAssetOption(CVanillaStorageOption::typeid)
{
#ifdef HANDLE
	_phVanillaStorageOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVanillaStorageOption = new boost::shared_ptr<QuantLib::VanillaStorageOption> (copy->GetShared());
        _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaStorageOption));
    }
}
Cephei::QL::Instruments::CVanillaStorageOption::CVanillaStorageOption (PLATFORM::Type^ t) : COneAssetOption(CVanillaStorageOption::typeid)
{
#ifdef HANDLE
	_phVanillaStorageOption = NULL;
#endif
	if (!t->IsSubclassOf(CVanillaStorageOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CVanillaStorageOption::CVanillaStorageOption (QuantLib::Handle<QuantLib::VanillaStorageOption>& childNative, Object^ owner)  : COneAssetOption(CVanillaStorageOption::typeid)
{
	_phVanillaStorageOption = &childNative;
	_ppVanillaStorageOption = &static_cast<boost::shared_ptr<QuantLib::VanillaStorageOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaStorageOption));
    _VanillaStorageOptionOwner = owner;
}
Cephei::QL::Instruments::CVanillaStorageOption::CVanillaStorageOption (QuantLib::Handle<QuantLib::VanillaStorageOption> childNative)  : COneAssetOption(CVanillaStorageOption::typeid)
{
	_phVanillaStorageOption = &childNative;
	_ppVanillaStorageOption = &static_cast<boost::shared_ptr<QuantLib::VanillaStorageOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaStorageOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CVanillaStorageOption::CVanillaStorageOption (QuantLib::VanillaStorageOption childNative)  : COneAssetOption(CVanillaStorageOption::typeid)
{
#ifdef HANDLE
	_phVanillaStorageOption = NULL;
#endif
	_ppVanillaStorageOption = new boost::shared_ptr<QuantLib::VanillaStorageOption> (new QuantLib::VanillaStorageOption (childNative));
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaStorageOption));
}
#endif

Cephei::QL::Instruments::CVanillaStorageOption::~CVanillaStorageOption ()
{
    if (_ppVanillaStorageOption != NULL)
    {
	    delete _ppVanillaStorageOption;
        _ppVanillaStorageOption = NULL;
    }
}
Cephei::QL::Instruments::CVanillaStorageOption::!CVanillaStorageOption ()
{
    if (_ppVanillaStorageOption != NULL)
    {
	    delete _ppVanillaStorageOption;
    }
}
QuantLib::VanillaStorageOption& Cephei::QL::Instruments::CVanillaStorageOption::GetReference ()
{
    if (_ppVanillaStorageOption == NULL) throw REFNEW NativeNullException ();
	return **_ppVanillaStorageOption;
}
boost::shared_ptr<QuantLib::VanillaStorageOption>& Cephei::QL::Instruments::CVanillaStorageOption::GetShared ()
{
    if (_ppVanillaStorageOption == NULL) throw REFNEW NativeNullException ();
	return *_ppVanillaStorageOption;
}
QuantLib::VanillaStorageOption* Cephei::QL::Instruments::CVanillaStorageOption::GetPointer ()
{
    if (_ppVanillaStorageOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppVanillaStorageOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VanillaStorageOption>& Cephei::QL::Instruments::CVanillaStorageOption::GetHandle ()
{
	if (_phVanillaStorageOption == NULL)
	{
		_phVanillaStorageOption = new Handle<QuantLib::VanillaStorageOption> (*_ppVanillaStorageOption);
	}
	return *_phVanillaStorageOption;
}
#endif
bool Cephei::QL::Instruments::CVanillaStorageOption::HasNative () 
{
	return (_ppVanillaStorageOption != NULL);
}

Boolean Cephei::QL::Instruments::CVanillaStorageOption::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppVanillaStorageOption)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IVanillaStorageOption^ Cephei::QL::Instruments::CVanillaStorageOption_Factory::Create (Cephei::QL::IBermudanExercise^ ex, Double capacity, Double load, Double changeRate, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CVanillaStorageOption ( ex,  capacity,  load,  changeRate,  QL_Pricer);
}
